/*
 * fml_master_iic.c
 *
 *  Created on: 2025年4月7日
 *      Author: 22332
 */
#include <stdio.h>
#include <ctype.h>
#include "string.h"
#include "cola_init.h"
#include "cola_device.h"
#include "cola_os.h"
#include "stdlib.h"
#include "fml_master_iic.h"
#include "shell.h"
#include "fml_gpio.h"

#define  TXBUFFER_LEN	8u
extern void logWrite(const char *fmt, ...);



static cola_device_t *g_pDevmaster = NULL;

uint8_t masterTxBuffer[TXBUFFER_LEN];
bool trans = true;

void fml_master_init(void)
{
	g_pDevmaster = cola_device_find("MASTER_IIC");
    if(g_pDevmaster)
        cola_device_open(g_pDevmaster, 0);
    for (uint8_t i = 0u; i < TXBUFFER_LEN; i++)
		masterTxBuffer[i] = i;
}

int fml_master_iic_write(uint8_t devAddr,uint8_t *buf,int len)
{
	int ret  =  0;
	cola_device_ctrl(g_pDevmaster, devAddr, NULL);
	ret = cola_device_write(g_pDevmaster, 0x00, buf, len);
	return ret;
}

int fml_master_iic_read(uint8_t devAddr,uint8_t reg,uint8_t *buf,int len)
{
	int ret  =  0;
	cola_device_ctrl(g_pDevmaster, devAddr, NULL);
	ret = cola_device_write(g_pDevmaster, 0x00, &reg, 1);
	if(ret == 1)
		ret = cola_device_read(g_pDevmaster, 0x00, buf, len);
	return ret;
}

uint8_t fml_master_read_tft_page_reg(uint8_t page,uint8_t reg)
{
	uint8_t pStartCmdSet[] = {0xC8,0x00,0x58,0xA8,0x18,0x01};
	uint8_t pPageSel[] = {0x00,0x00,page};
	uint8_t pPassWord[] = {0xFD,0x00,0xAA};
	uint8_t pReg[] = {reg,0x00};
	uint8_t readValue = 0x00;
	cola_device_ctrl(g_pDevmaster, SLAVE_ADDR_TFT, NULL);
	cola_device_write(g_pDevmaster,0,pStartCmdSet,sizeof(pStartCmdSet));
	cola_device_write(g_pDevmaster,0,pPageSel,sizeof(pPageSel));
	cola_device_write(g_pDevmaster,0,pPassWord,sizeof(pPassWord));
	cola_device_write(g_pDevmaster,0,pReg,sizeof(pReg));
	cola_device_ctrl(g_pDevmaster, SLAVE_ADDR_TFT, NULL);
	cola_device_read(g_pDevmaster,0,&readValue,1);
	return readValue;
}
#if 0
static int string_to_number(const char *str) {
    if (!str || *str == '\0') return -1;
    long result = 0;
    char *end_ptr;
    int base = 10;
    if (str[0] == '0' && tolower(str[1]) == 'x') {
    	base = 16;
    }
    result = strtol(str, &end_ptr, base);
    return result;
}


int masteri2c(int argc, char *argv[]) {
	if (argc < 4) {
		logWrite("para length must greater than 3\r\n");
		return 0;
	}
	int ret = 0;
	int i = 0, len = argc - 3;

	int address,slaveAddr=0x00;
	uint8_t buf[SHELL_PARAMETER_MAX_NUMBER];

	bool bEnableRead = false;
	bool bEnableWrite = false;
	char *pDevStrName = argv[1];
	int mode = atoi(argv[2]);
	cola_device_t *pDev = NULL;

	//logWrite("argc=%d\r\n",argc);
	memset(buf, 0,sizeof(buf));

	if(strcmp(pDevStrName,"max25512") == 0)
	{
		slaveAddr = SLAVE_ADDR_MAX25512;
		pDev = cola_device_find("max25512_IIC");
		if(pDev == NULL)return 0;

		if(mode == 0)
			bEnableRead = true;
		else if(mode == 1)
			bEnableWrite = true;
	}
	else if(strcmp(pDevStrName,"pmic") == 0)
	{
		slaveAddr = SLAVE_ADDR_PMIC;
		pDev = cola_device_find("PMIC_IIC");
		if(pDev == NULL)return 0;

		if(mode == 0)
			bEnableRead = true;
		else if(mode == 1)
			bEnableWrite = true;
	}
	else if(strcmp(pDevStrName,"tft") == 0)
	{
		slaveAddr = SLAVE_ADDR_TFT;
		pDev = cola_device_find("TFT_IIC");
		if(pDev == NULL)return 0;

		if(mode == 0)
			bEnableRead = true;
		else if(mode == 1)
			bEnableWrite = true;
	}
	else if(strcmp(pDevStrName,"backlight") == 0)
	{
		extern void fml_gpio_write(uint8_t pos,bool status);
		extern bool fml_Write_ISET(uint8_t value);
		logWrite("backlight=%d\r\n",mode);
		fml_gpio_write(1,mode);
	}
	else if(strcmp(pDevStrName,"iset") == 0)
	{
		extern void fml_gpio_write(uint8_t pos,bool status);
		extern bool fml_Write_ISET(uint8_t value);
		logWrite("fml_Write_ISET=%d\r\n",mode);
		fml_Write_ISET(0x02);
	}
	else if(strcmp(pDevStrName,"bist") == 0)
	{
		extern void fml_gpio_write(uint8_t pos,bool status);
		logWrite("OUT_TFT_BIST=%d\r\n",mode);
		fml_gpio_write(4,mode);
	}
	else if(strcmp(pDevStrName,"pdb") == 0)
	{
		extern void fml_gpio_write(uint8_t pos,bool status);
		logWrite("pdb=%d\r\n",mode);
		fml_gpio_write(2,mode);
	}
	else if(strcmp(pDevStrName,"pmic_en") == 0)
	{
		logWrite("pmic en=%d\r\n",mode);
		fml_gpio_write(OUT_BL_EN,false);
	}
	else
	{
		//logWrite("not support\r\n");
		extern uint8_t fml_tft_read_page_reg(uint8_t page,uint8_t reg);
		uint8_t page = string_to_number(argv[2]);
		uint8_t reg = string_to_number(argv[3]);
#if 0
		logWrite("read = 0x%02X\r\n",fml_tft_read_page_reg(page,reg));
#else
		logWrite("read = 0x%02X\r\n",fml_master_read_tft_page_reg(page,reg));
#endif
	}

	if(bEnableWrite)
	{
		for (i = 0; i < len; i++)
			buf[i] = string_to_number(argv[3 + i]);
		address = buf[0];

		logWrite("Write[len=%d][addr=0x%02X]:",len,address);
		for (i = 0; i < len; i++) {
			logWrite("%02X ", buf[i]);
		}
		logWrite("\r\n");
#if 0
		ret = cola_device_write(pDev, address, buf + 1, len - 1);
#else
		ret = fml_master_iic_write(slaveAddr, buf, len);
#endif
		if (ret == 0) {
			logWrite("write err\r\n");
		}
	}
	else if(bEnableRead)
	{
		address = string_to_number(argv[3]);
		if(strcmp(pDevStrName,"tft") == 0 && (address > 0xFF))
		{
			address = string_to_number(argv[3]);
			len 	= string_to_number(argv[4]);
			logWrite("Read[len=%d][addr=0x%04X]:",len,address);
#if 0
			ret = cola_device_read(pDev, address, buf, len);
#else
			ret = fml_master_iic_read(slaveAddr, address, buf, len);
#endif
			if (ret == 0) {
				logWrite("read err\r\n");
			}else{
				for (i = 0; i < len; i++) {
					logWrite("%02X ", buf[i]);
				}
				logWrite("\r\n");
			}
		}
		else
		{
			address = string_to_number(argv[3]);
			len 	= string_to_number(argv[4]);
			if(len > SHELL_PARAMETER_MAX_NUMBER)
			{
				logWrite("Warn:len[%d] >SHELL_PARAMETER_MAX_NUMBER[%d],set len to %d\r\n",len,SHELL_PARAMETER_MAX_NUMBER,SHELL_PARAMETER_MAX_NUMBER);
				len = SHELL_PARAMETER_MAX_NUMBER;
			}
			logWrite("Read[len=%d][addr=0x%02X]:",len,address);
#if 0
			ret = cola_device_read(pDev, address, buf, len);
#else
			ret = fml_master_iic_read(slaveAddr, address, buf, len);
#endif
			if (ret == 0) {
				logWrite("read err\r\n");
			}else{
				for (i = 0; i < len; i++) {
					logWrite("%02X ", buf[i]);
				}
				logWrite("\r\n");
			}
		}
	}
	return 0;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), masteri2c, masteri2c, masteri2c,masteri2c);


#endif



